var bmap = require('../../libs/bmap-wx.min.js');
var wxMarkerData = [];
var app = getApp();
Page({
  data: {
    markers: [],
    latitude: '',
    longitude: '',
    placeData: {},
    scale:"6",
    addMapData: [],
    scrollLeft: 0,
    choiceCurrentData: 0,
    pageProjectData: [],
    commonUrl: app.globalData.commonUrl,
    createTimeData: [],
    swiperIndex: 0,
  },
  toLogin: function() {
    wx.navigateTo({
      url: '../login/login'
    })
  },
  regionchange(e) {

  },
  onReady: function(e) {
    this.mapCtx = wx.createMapContext('myMap')
  },
  getCenterLocation: function() {
    this.mapCtx.getCenterLocation({
      success: function(res) {
        console.log(res.longitude);
        console.log(res.latitude)
      }
    })
  },
// 
// 
  foGoodPlaceDetail: function(e) {
    var proid = e.currentTarget.dataset.proid;
    wx.navigateTo({
      url: '../goodPlaceDetail/goodPlaceDetail?proId=' + proid
    })
  },
  callPhone: function() {
    wx.makePhoneCall({
      phoneNumber: '400555999'
    })
  },
  swiperChange(e) {
    var that = this;
    // console.log(this.data.pageProjectData[e.detail.current].latitude)
    this.setData({
      swiperIndex: e.detail.current,
      latitude: this.data.pageProjectData[e.detail.current].latitude,
      longitude: this.data.pageProjectData[e.detail.current].longitude
    });
    if (e.detail.current == this.data.choiceCurrentData) {
      return;
    } else {
      this.setData({
        choiceCurrentData: e.detail.current
      });
      this.changeMarkerColor(this.data.markers, e.detail.current);
    }
  },
  switchTab: function(e) {
    // var id=Math.ceil(e.detail.scrollLeft/180);
    // if(id==this.data.choiceCurrentData){
    //     return;
    // }else{
    //     this.setData({
    //         choiceCurrentData:id,
    //     });
    //     this.changeMarkerColor(this.data.markers, id);
    // }
  },
  makertap: function(e) {
    var that = this;
    var id = e.markerId;
    if (id == this.data.choiceCurrentData) {
      return;
    } else {
      this.setData({
        choiceCurrentData: id,
        swiperIndex: id,
        // scrollLeft:165*id
      });
      that.changeMarkerColor(that.data.markers, id);
    }

  },
  getPageAreaListData: function(long, lat) {
    var that = this;
    let params = {
      userToken: app.globalData.openid,
      startLong: long,
      startLat: lat,
      distance: '',
    };
    app.handlerPost('/api/project/pageAreaList.do', params, "GET").then((res) => {
      if (res.data.code == "SUCCESS") {
        var mapdata = res.data.data.items;

      }
    }).catch((errMsg) => {
      console.log(errMsg);
    });
  },
  getPageProjectData: function() {
    wx.showLoading({
      title: '加载中',
    });
    let params = {
      userToken: app.globalData.openid,
      'page.rows': 1000
    };
    app.handlerPost('/api/project/pageProject.do', params, "GET").then((res) => {
      if (res.data.code == "SUCCESS") {
        var data = res.data.data.items;
        var markers = data.map(function(value, key) {
          var obj = {
            id: key,
            latitude: Number(value.latitude),
            longitude: Number(value.longitude),
            iconPath: '../../img/icon_like.png',
            iconTapPath: '../../img/icon_like.png',
            width: '50',
            height: '50',
            areaCode: value.areaCode,
            proId: value.id,
            proName: value.proName,
            proAddress: value.proAddress,
            videoNum: value.videoNum,
            posterImg: value.posterImg,
            tags: value.tags,
            sayNumber: value.sayNum
          };
          return obj;
        });
        markers[0].iconPath = '../../img/icon_like_hl.png';
        var createTime = data.map(function(value, key) {
          var obj = {
            createTime: new Date(value.videoDate).Format("yyyy-MM-dd")
          };
          return obj;
        });
        this.setData({
          pageProjectData: data,
          createTimeData: createTime,
          markers: markers
        });
      }
      app.fetchAddressList();
    }).catch((errMsg) => {
      console.log(errMsg);
    });
  },



  onShareAppMessage: function(res) {
    if (res.from === 'button') {
      console.log(res.target)
    }
    return {
      title: 'GOODPLACE',
      path: '/pages/index/index',
      success: function(res) {
        console.log(res);
      },
      fail: function(res) {
        console.log(res);
      }
    }
  },
 
  onLoad: function() {
   
    wx.login({
      success: function(res) {
        app.login({
          code: res.code
        });
      }
      
    });
    var that = this;
    
    this.getPageProjectData();
    wx.getLocation({
      type: 'wgs84',
      success: function(res) {
        console.log(res)
        var latitude = res.latitude;
        var longitude = res.longitude;
        var speed = res.speed
        var accuracy = res.accuracy
        wx.setStorageSync("latitude", latitude)
        wx.setStorageSync("longitude", longitude)
        that.setData({
          speed: speed,
          accuracy: accuracy,
          latitude: latitude,
          longitude: longitude
        });
        that.getPageAreaListData(that.data.longitude,that.data.latitude);
      }
    });
  },
  moveToLocation: function () {
    let longitude= wx.getStorageSync("longitude")
    let latitude= wx.getStorageSync("latitude")
    this.mapCtx.moveToLocation()
    this.setData({
      scale: 15,
      latitude:latitude,
      longitude: longitude
    })

  },
  changeMarkerColor: function(data, id) {
    var that = this;
    for (var i = 0; i < data.length; i++) {
      if (i === id) {
        data[i].iconPath = "../../img/icon_like_hl.png";
      } else {
        data[i].iconPath = "../../img/icon_like.png";
      }
      this.data.markers[i] = data[i];
    }
    that.setData({
      markers: this.data.markers
    });
  }
});